/* DwrConfig.java
 *
 * Copyright 2006, Tim Dwelle.
 *
 * Licensed under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in
 * compliance with the License.  You may obtain a copy of
 * the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in
 * writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 * CONDITIONS OF ANY KIND, either express or implied.  See
 * the License for the specific language governing
 * permissions and limitations under the License.
 *
 */

package dowry.dwr;

import java.util.*;
import javax.servlet.*;

/**
 * DwrConfig is a wrapper to the actual servlet config,
 * allowing us to modify our servlet config's initialization
 * parameters.
 *
 */
public class DwrConfig implements ServletConfig
{
    private ServletConfig cfg;
    private Hashtable     params;

    /**
     * Construct a wrapper to the provided servlet config
     * instance.
     *
     * @param cfg  the servlet config to be wrapped
     *
     */
    public DwrConfig(ServletConfig cfg)
    {
        this.cfg = cfg;

        this.params = new Hashtable();
        Enumeration e = cfg.getInitParameterNames();
        while (e.hasMoreElements())
        {
            String name = (String) e.nextElement();
            String value = cfg.getInitParameter(name);
            params.put(name, value);
        }
    }

    /**
     * Returns a String containing the value of the named
     * initialization parameter, or null if the parameter
     * does not exist.
     *
     * @param name  a String specifying the name of the
     *              initialization parameter
     *
     * @return      a String containing the value of the
     *              initialization parameter
     *
     */
    public String getInitParameter(String name)
    {
        return (String) params.get(name);
    }

    /**
     * Stores the provided String value as a named
     * initialization parameter.
     *
     * @param name  a String specifying the name of the
     *              initialization parameter
     *
     * @param value a String containing the value of the
     *              initialization parameter
     *
     */
    public void setInitParameter(String name, String value)
    {
        params.put(name, value);
    }

    /**
     * Returns the names of the servlet's initialization
     * parameters as an Enumeration of String objects, or
     * an empty Enumeration if the servlet has no
     * initialization parameters.
     *
     * @return  an Enumeration of String objects containing
     *          the names of the servlet's initialization
     *          parameters
     *
     */
    public Enumeration getInitParameterNames()
    {
        return params.keys();
    }

    /**
     * Returns a reference to the ServletContext in which
     * the caller is executing.
     *
     * @return  a ServletContext object, used by the caller
     *          to interact with its servlet container
     *
     */
    public ServletContext getServletContext()
    {
        return cfg.getServletContext();
    }

    /**
     * Returns the name of this servlet instance. The name
     * may be provided via server administration, assigned
     * in the web application deployment descriptor, or for
     * an unregistered (and thus unnamed) servlet instance
     * it will be the servlet's class name.
     *
     * @return  the name of the servlet instance
     *
     */
    public String getServletName()
    {
        return cfg.getServletName();
    }
}
